package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.MedDevClass;
import service.MedDevClassService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import pojo.MedDevClass;
import service.MedDevClassService;

/**
 * 医疗器械分类管理控制器
 */
@Controller
@RequestMapping(value = "/medDevClass")
@CrossOrigin(origins = "*")
@Api(tags = "医疗器械分类管理接口")
public class MedDevClassController {
    @Autowired
    private MedDevClassService medDevClassService;

    /**
     * 获取医疗器械分类列表（JSON格式）
     */
    @RequestMapping(value = "/list1", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取医疗器械分类列表", notes = "返回所有医疗器械分类的JSON数据")
    public List<MedDevClass> getMedDevClassList1() {
        return medDevClassService.findAllMedDevClass();
    }

    /**
     * 获取单个医疗器械分类详情
     */
    @GetMapping(value = "/detail/{id}")
    @ResponseBody
    @ApiOperation(value = "获取医疗器械分类详情", notes = "根据ID获取单个医疗器械分类的详细信息")
    @ApiImplicitParam(name = "id", value = "医疗器械分类ID", required = true, dataType = "int", paramType = "path")
    public ResponseEntity<MedDevClass> getMedDevClassDetail(@PathVariable("id") Integer id, Model model) {
        MedDevClass medDevClass = medDevClassService.findMedDevClassById(id);
        if (medDevClass == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(medDevClass);
    }

    /**
     * 跳转到添加医疗器械分类页面
     */
    @RequestMapping(value = "/input", method = RequestMethod.GET)
    @ApiOperation(value = "跳转到添加页面", notes = "返回添加医疗器械分类的表单页面")
    public String inputMedDevClass(Model model) {
        model.addAttribute("action", "create");
        return "inputMedDevClass";
    }

    /**
     * 跳转到修改医疗器械分类页面
     */
    @RequestMapping(value = "/input/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "跳转到修改页面", notes = "根据ID获取医疗器械分类并返回修改表单页面")
    @ApiImplicitParam(name = "id", value = "医疗器械分类ID", required = true, dataType = "int", paramType = "path")
    public String inputMedDevClassById(@PathVariable("id") Integer id, Model model) {
        MedDevClass medDevClass = medDevClassService.findMedDevClassById(id);
        model.addAttribute("model", medDevClass);
        model.addAttribute("action", "update");
        return "inputMedDevClass";
    }

    /**
     * 添加医疗器械分类
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation(value = "添加医疗器械分类", notes = "提交表单数据创建新的医疗器械分类")
    @ApiImplicitParam(name = "model", value = "医疗器械分类实体", required = true, dataType = "MedDevClass")
    public String create(MedDevClass model) {
        medDevClassService.saveMedDevClass(model);
        return "redirect:/medDevClass/list";
    }

    /**
     * 更新医疗器械分类
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "更新医疗器械分类", notes = "提交表单数据更新已存在的医疗器械分类")
    @ApiImplicitParam(name = "model", value = "医疗器械分类实体", required = true, dataType = "MedDevClass")
    public String update(MedDevClass model) {
        medDevClassService.saveMedDevClass(model);
        return "redirect:/medDevClass/list";
    }

    /**
     * 删除医疗器械分类
     */
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    @ApiOperation(value = "删除医疗器械分类", notes = "根据ID删除对应的医疗器械分类")
    @ApiImplicitParam(name = "id", value = "医疗器械分类ID", required = true, dataType = "int", paramType = "path")
    public String del(@PathVariable("id") Integer id, Model model) {
        medDevClassService.delMedDevClassById(id);
        return "redirect:/medDevClass/list";
    }

    /**
     * 根据参数搜索医疗器械分类
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "搜索医疗器械分类", notes = "根据指定参数搜索医疗器械分类")
    @ApiImplicitParam(name = "medDevClass", value = "搜索条件", required = true, dataType = "MedDevClass")
    public String search(MedDevClass medDevClass, HttpServletRequest request, Model model) {
        model.addAttribute("search", medDevClass);
        model.addAttribute("medDevClassList", medDevClassService.findMedDevClassByParams(medDevClass));
        return "medDevClassList";
    }
}

//@Controller
//@RequestMapping(value = "/medDevClass")
//public class MedDevClassController {
//    @Autowired
//    private MedDevClassService medDevClassService;
//
//    // 列表
//    @RequestMapping(value = "/list1", method = RequestMethod.GET)
//    @ResponseBody
//    public List<MedDevClass> getMedDevClassList1() {
//        return medDevClassService.findAllMedDevClass();
//    }
//
//    // 添加
//    @RequestMapping(value = "/create1", method = RequestMethod.POST)
//    public ResponseEntity<MedDevClass> create1(@RequestBody MedDevClass model) {
//        MedDevClass savedMedDevClass = medDevClassService.saveMedDevClass(model);
//        return new ResponseEntity<>(savedMedDevClass, HttpStatus.CREATED);
//    }
//
//    // 修改
//    @GetMapping(value = "/input/{id}")
//    public ResponseEntity<MedDevClass> inputMedDevClassById1(@PathVariable("id") Integer id) {
//        MedDevClass medDevClass = medDevClassService.findMedDevClassById(id);
//        if (medDevClass != null) {
//            return new ResponseEntity<>(medDevClass, HttpStatus.OK);
//        } else {
//            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
//        }
//    }
//
//    @RequestMapping(value = "/update1", method = RequestMethod.POST)
//    public ResponseEntity<MedDevClass> update1(@RequestBody MedDevClass model) {
//        medDevClassService.saveMedDevClass(model);
//        return new ResponseEntity<>(model, HttpStatus.OK);
//    }
//
//    // 删除
//    @RequestMapping(value = "/del1/{id}", method = RequestMethod.POST)
//    public ResponseEntity<Void> del1(@PathVariable("id") Integer id) {
//        medDevClassService.delMedDevClassById(id);
//        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
//    }
//
//    // 查询
//    @RequestMapping(value = "/search1", method = RequestMethod.POST)
//    public ResponseEntity<List<MedDevClass>> search1(@RequestBody MedDevClass medDevClass) {
//        List<MedDevClass> result = medDevClassService.findMedDevClassByParams(medDevClass);
//        return new ResponseEntity<>(result, HttpStatus.OK);
//    }
//}




